// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.server.model.dao.impl;

import java.util.List;
import net.taviscaron.drivenotes.server.model.dao.PlaceDAO;
import net.taviscaron.drivenotes.shared.model.entity.Place;
import net.taviscaron.drivenotes.shared.model.entity.User;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Place DAO impl
 * @author Andrei Senchuk
 */
public class PlaceDAOImpl extends HibernateDaoSupport implements PlaceDAO {

    public Place findById(Long id) {
        return getHibernateTemplate().load(Place.class, id);
    }

    public void save(Place place) {
        getHibernateTemplate().saveOrUpdate(place);
    }

    public void remove(Place obj) {
        getHibernateTemplate().delete(obj);
    }

    @SuppressWarnings("unchecked")
    public List<Place> findByOwnerId(Long id) {
        return (List<Place>)getHibernateTemplate().find("from Place place where user = ? order by place.name", String.valueOf(id));
    }

    public Place findByOwnerAndId(User owner, Long id) {
        try {
            List list = getHibernateTemplate().find("from Place where author = ? and id = ?", owner, id);
            return (Place)list.get(0);
        } catch(IndexOutOfBoundsException e) {
            return null;
        }
    }
}
